home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 223_01 / prntf.c < prev    next >
Text File  |  1980-01-01  |  3KB  |  120 lines

  1.  
  2. /*
  3. ** prntf("format string"[, arg, arg, ...]) -- formatted print 
  4. **        operates as described by Kernighan & Ritchie with
  5. **        the following exceptions:
  6. **
  7. **        - only d, c, and s formats supported
  8. **        - maximum field-width specification of 9
  9. **        - no field seperator specification
  10. **
  11. **    Modified version of J. E. Hendrix's printf distributed
  12. **    with Small-C Version 2.0. This version uses putchar for
  13. **    for output and is renamed to prntf to avoid mutiple 
  14. **    global defines. It is designed to occupy minimum code
  15. **    space and can be used interchangeably with printf(),as
  16. **    long as the exceptions are taken into consideration.
  17. **
  18. **            F. A. Scacchitti  10/7/84
  19. */ 
  20.  
  21. #include <stdio.h>
  22.  
  23. /*
  24. **    Making the following variables static greatly reduces size 
  25. **    while increasing speed. However, if static globals aren't
  26. **    available on your compiler, the prntf.mac file must be
  27. **    edited to remove the additional colon generated by the
  28. **    compiler to ensure no problems occur with multiple global
  29. **    definitions. An alternative to this method would involve
  30. **    renaming all the static variable with some little used
  31. **    prefix. (eg. qqq, xxx, ppp)
  32. */
  33.  
  34.  
  35. static  int i, width, len, *nxtarg, sz;
  36. static  char *ctl, *cx, c, right, str[7], *sptr, pad, sgn;
  37.  
  38. prntf(argc) int argc; {
  39.  
  40.   i = CCARGC();   /* fetch arg count from A reg first */
  41.   nxtarg = &argc + i - 1;
  42.   ctl = *nxtarg;
  43.  
  44.   while(c = *ctl++) {
  45.     if(c != '%') {
  46.       putchar(c);
  47.       continue;
  48.     }
  49.     if(*ctl == '%') {
  50.       putchar(*ctl++);
  51.       continue;
  52.     }
  53.     cx = ctl;
  54.     right = 1;
  55.     pad = ' ';
  56.  
  57.     if(*cx == '-') {
  58.       right = 0;
  59.       ++cx;
  60.     }
  61.     if(*cx == '0') {
  62.       pad = '0';
  63.       ++cx;
  64.     }
  65.  
  66.     width = *cx - '0';
  67.     if(width < 1 || width >9)
  68.       width = 0;
  69.     else
  70.       cx++;
  71.  
  72.     sptr=str; c = *cx++; i = *(--nxtarg);
  73.  
  74.     switch(c) {
  75.  
  76.     case 'd': sz = 7;
  77.  
  78.           if(i < 0) {
  79.             i = -i;
  80.             sgn='-';
  81.           }
  82.           else sgn=' ';
  83.   
  84.           str[--sz] = NULL;
  85.  
  86.           while(sz) {
  87.             str[--sz] = (i % 10 + '0');
  88.             if((i = i / 10) == 0) break;
  89.             }
  90.           if(sz) str[--sz] = sgn;
  91.           while(sz > 0) str[--sz]=' ';
  92.  
  93.           break;
  94.     case 'c': str[0] = i;
  95.           str[1] = NULL;
  96.           break;
  97.     case 's': sptr = i;
  98.           break;
  99.     default : continue;
  100.     }
  101.  
  102.   ctl = cx; /* accept conversion spec */
  103.     if(c == 'd')
  104.       while(*sptr==' ') ++sptr;
  105.  
  106.     len = -1;
  107.     while(sptr[++len]); /* get length */
  108.  
  109.     if(right)
  110.       while(((width--) - len) > 0) putchar(pad);
  111.     while(len) {
  112.       putchar(*sptr++);
  113.       --len;
  114.       --width;
  115.     }
  116.     while(((width--) - len) > 0) putchar(pad);
  117.     }
  118.   }
  119.  
  120.